﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace LED_Editor.Controls
{
    public static  class TimeHelper
    {
        public static string GetUserFriendlyString(int milliseconds)
        {
            //Console.WriteLine(ts.ToString());
            double ms = (double)milliseconds;
            //double result = 0f;
            string resultString = string.Empty;  
            //StringBuilder sb = new StringBuilder();
            if (ms == 0)
            {
                resultString+="0";
            }
            //else if (ms > 0 && ms < 1000)
            //{
            //    result = ms;
            //    string format = string.Empty;
            //    if (ms % 100 == 0)
            //    {
            //        format = "{0:F1}";
            //    }
            //    else if (ms % 10 == 0)
            //    {
            //        format = "{0:F2}";
            //    }
            //    else
            //    {
            //        format = "{0:F3}";
            //    }
            //    sb.Append(string.Format(CultureInfo.InvariantCulture, format, ms / 1000));
            //    sb.Append("s");

            //}
            //else if (ms >= 1000 && ms < 60000)
            //{
            //    result = ms;
            //}
            //else if (ms >= 60000 && ms < 60 * 60*1000 && ms % 60 == 0)
            //{
            //    result = ms / 60;
            //}
            //else if (ms >= 60 * 60 && ms < 60 * 60 * 24 && ms % (60 * 24) == 0)
            //{
            //    result = ms / (60 * 24);//seconds / (60 * 24);
            //}
            else 
            {
                TimeSpan ts = new TimeSpan(0, 0, 0, 0, milliseconds);

                int hours = ts.Hours;
                int minutes = ts.Minutes;
                int seconds = ts.Seconds ;
                int milliSeconds = ts.Milliseconds ;

                if (hours!=0)
                {
                     resultString+=hours;
                     resultString+="h";
                }
                if (minutes != 0)
                {
                     resultString+=minutes;
                     resultString+="m";
                }
                if (seconds != 0)
                {
                     resultString+=seconds;
                     resultString+="s";
                }
                if (milliSeconds != 0)
                {
                     resultString+=milliSeconds;
                    resultString+="ms";
                }
            }
            //else return milliseconds;//(float)Math.Round((double)result, 2);

            return resultString;
        }
    }
}
